package activities;

import handler.Utilities;

import java.util.ArrayList;
import java.util.HashMap;

import model.Album;
import model.Artist;
import model.MusicDB;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.lecteurmp3.R;
/**
 * Class of the activity showing the list of Album
 * @author jean
 *
 */
public class AlbumActivity extends ListActivity {
	
	private ArrayList<Album> albumList = new ArrayList<Album>();
	private ArrayList<HashMap<String, String>> albumListView;
	private ListAdapter adapter;
	private ListView lv;
	private boolean run = false;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		// We retrieve the album list to show...
		albumListView = getAlbumListView(getAlbumList());
		
		// Add items to ListView
		adapter = new SimpleAdapter(this, albumListView,
												R.layout.list_item_bis,
												new String[] { "album"},
												new int[] {R.id.titleItem });

		setListAdapter(adapter);
		
		// selecting single ListView item
		lv = getListView();
		// listening to single listitem click
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				getParent().getIntent().putExtra("albumId", albumList.get(position).getId());
				finish();
			}
		});
    }
	
	public void onResume() {
		
		super.onResume();
		if(run) {
			albumListView = getAlbumListView(getAlbumList());
			
			// Add items to ListView
			adapter = new SimpleAdapter(this, albumListView,
													R.layout.list_item_bis,
													new String[] { "album"},
													new int[] {R.id.titleItem });

			setListAdapter(adapter);
			// selecting single ListView item
			lv = getListView();
		} else run = true;
		Toast.makeText(getApplicationContext(), "Resume...", Toast.LENGTH_SHORT).show();
	}
	
	public void onRestart() {
		super.onRestart();
		Toast.makeText(getApplicationContext(), "Restart...", Toast.LENGTH_SHORT).show();
	}
	
	/**
	 * Retrieve the album to show, in function intent
	 * @return ArrayList<Album>
	 */
	public ArrayList<Album> getAlbumList() {
		MusicDB musicDb = new MusicDB(this);
		musicDb.open();
		
		Artist artistCourant;
		
		Bundle extras = getIntent().getExtras();
		
		if(extras != null) {
			if(extras.containsKey("artistId")) {
				Toast.makeText(getApplicationContext(), "resume...", Toast.LENGTH_SHORT).show();
				artistCourant = musicDb.cursorToArtist(musicDb.getAlbum(extras.getInt("artistId"))).get(0);
				albumList = musicDb.cursorToAlbum(musicDb.getAlbumByArtist(artistCourant));
			} else {
				albumList = musicDb.cursorToAlbum(musicDb.getAllAlbum());
			}
		} else {
			albumList = musicDb.cursorToAlbum(musicDb.getAllAlbum());
		}
		musicDb.close();
		return albumList;
	}
	
	/**
	 * Get the list of album normalized for the Adapter
	 * @param albumList
	 * @return ArrayList<HashMap<String, String>>
	 */
	public ArrayList<HashMap<String, String>> getAlbumListView(ArrayList<Album> albumList) {
		
		ArrayList<HashMap<String, String>> albumListView = new ArrayList<HashMap<String,String>>();
		Utilities util = new Utilities();
		
		for(int i=0;i<albumList.size();i++) {
			Album a = albumList.get(i);
			albumListView.add(util.AlbumToHashMap(a));
		}
		return albumListView;
	}
	
	

}
